package io.gps.datatransfer.codec;

import io.gps.datatransfer.command.GpsMsg;
import io.gps.datatransfer.utils.SysDef;
import io.gps.datatransfer.utils.Utils;
import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import java.io.IOException;



/**
 * 设备服务器 回话处理逻辑
 *
 * @author bcs
 * @version 2.0, $Date: 2020-11-26 14:00:29 $
 */

public class DevHandler extends IoHandlerAdapter {

	private static Logger logger = Logger.getLogger(DevHandler.class);

	@Override
	public void sessionOpened(IoSession session) {
	}
	@Override
	public void sessionClosed(IoSession session) throws Exception {
		  
	}
	@Override
	public void messageReceived(IoSession session, Object message) {
		lstTime=System.currentTimeMillis();
		try {
			boolean t=Utils.checkTime();
			if(t){
				SysDef.setSession(((GpsMsg)message).getSim(),session);
				SysDef.allMsgQuery.add(message);
			}
		} catch (Exception e) {
			logger.error("缓存数据错误！",e);
		}
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status) {
		session.closeNow();
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) {
		if(!(cause instanceof IOException)){
			logger.error("handler exceptionCaught"+cause.getMessage(),cause);
		}
	}
	
	@Override
	public void messageSent(IoSession session, Object message) {

	}

	long lstTime;
	public long getLastRecvTime(){
		return lstTime;
	}

}
